package cn.com.self.apis.portal.controller;

import cn.com.self.apis.files.entity.Files;
import cn.com.self.apis.portal.entity.AdvertisementEntity;
import cn.com.self.apis.portal.entity.FileRelationEntity;
import cn.com.self.apis.portal.service.intf.IAdvertisementService;
import cn.com.self.apis.portal.service.intf.IFileRelationService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import static cn.com.self.apis.portal.util.ConstantUtils.FILE_TYPE_LUNBO;

/**
 * @author CRL
 * @date 2021/1/9 16:23
 */
@Controller
@RequestMapping("/carouselAdver")
@Api(tags = "首页轮播图信息")
public class AdvertisementController {

    @Autowired
    IAdvertisementService advertisementService;

    @Autowired
    IFileRelationService fileRelationService;

    @PostMapping(value = "/create")
    @ApiOperation(value = "创建首页轮播图")
    public ResponseEntity create(@RequestBody AdvertisementEntity requestBody) {
        requestBody.setCreateTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
        requestBody.setUpdateTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
        AdvertisementEntity advertisementEntity = this.advertisementService.create(requestBody);
        Long objId = advertisementEntity.getId();
        List<Files> files = requestBody.getFiles();
        if (files != null) {
            for (Files fileId : files) {
                FileRelationEntity relationEntity = new FileRelationEntity();
                relationEntity.setFileId(String.valueOf(fileId.getId()));
                relationEntity.setObjId(String.valueOf(objId));
                relationEntity.setType(FILE_TYPE_LUNBO);
                fileRelationService.create(relationEntity);
            }
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(advertisementEntity)));
    }

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "删除首页轮播图")
    public ResponseEntity deletes(@PathVariable("id") String id) {
        this.advertisementService.delete(id);
        //删除附件关系表
        fileRelationService.deleteByObjId(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    @PostMapping(value = "/update/{id}")
    @ApiOperation(value = "更新首页轮播图")
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody AdvertisementEntity requestBody) {
        AdvertisementEntity advertisementEntity = this.advertisementService.load(id);
        CopyUtils.copyProperties(requestBody, advertisementEntity);
        requestBody.setUpdateTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
        AdvertisementEntity info = this.advertisementService.update(advertisementEntity);

        //更新附件信息
        List<Files> files = requestBody.getFiles();
        if (files != null) {
            fileRelationService.deleteByObjId(id);
            for (Files file : files) {
                FileRelationEntity relationEntity = new FileRelationEntity();
                relationEntity.setFileId(String.valueOf(file.getId()));
                relationEntity.setObjId(id);
                fileRelationService.create(relationEntity);
            }
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(info)));
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "分页获取首页轮播图")
    public ResponseEntity getUserPage(HttpServletRequest request, Pageable page) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page", "sort","access_token");
        ICustomPage<AdvertisementEntity> pageData = this.advertisementService.page(queryParams, page);
        //封装文件对象
        for (AdvertisementEntity pageDatum : pageData.getRows()) {
            Long id = pageDatum.getId();
            List<Files> files = fileRelationService.findFilesByObjId(id);
            if (files != null) {
                pageDatum.setFiles(files);
            }
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));
    }

    @GetMapping(value = {"/load/{id}"})
    @ApiOperation(value = "根据id获取首页轮播图")
    public ResponseEntity loadId(@PathVariable("id") String id, HttpServletRequest request) {
        AdvertisementEntity advertisementEntity = this.advertisementService.load(id);
        //封装文件对象
        Long objId = advertisementEntity.getId();
        List<Files> files = fileRelationService.findFilesByObjId(objId);
        advertisementEntity.setFiles(files);
        return ResponseEntity.status(HttpStatus.OK).body(new cn.com.self.apis.core.vo.ResultModel<>(advertisementEntity));
    }

}
